<template>
  <div class="publicBack" :style="{ height: winHeight + 'px' }">
    <c-header :showSelect="false"></c-header>
    <div class="showSelet" :style="{ top: winHeight > 1000 ? '42%' : '35%' }">
      <div class="login_title">
        <span class="forty_seven">Hi,你好！</span>
        <span class="thirty_six">欢迎进入VTE管理系统</span>
        <span class="twenty">Welcome to the Smart Park Management System </span>
      </div>
      <div class="login_btn">
        <div class="login_describe">用户登录</div>
        <div class="login_input">
          <el-input placeholder="账号" v-model="logonForm.username" clearable @blur="blurGetList"></el-input>
          <el-input placeholder="密码" v-model="logonForm.password" clearable show-password></el-input>
          <el-select multiple v-model="choseList" filterable placeholder="请选择科室">
            <el-option v-for="item in options" :key="item.id" :label="item.deptName" :value="item.id" />
          </el-select>
        </div>
        <el-button type="primary" round @click="goback" :loading="loading" class="login_btns">登录</el-button>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, reactive, getCurrentInstance } from "vue";
import md5 from "js-md5";
import Vue from "vue";
import CHeader from "../../components/title";
import { startSign, loginAdmin, login } from "../../api/login";
import { useRoute, useRouter } from "@/hooks/use-router";
import { Message } from "element-ui";

const route = useRoute(); //路由跳转
const router = useRouter(); //路由跳转

let loading = ref(false);
let choseList = ref([]);
let options = ref([]);
let deptId = ref("");

let logonForm = ref({
  username: "admin",
  password: "12345678",
});
const winHeight = ref(800);

onMounted(() => {
  winHeight.value = route.query.loginHeight;
});
// 输入姓名时候，失去焦点
const blurGetList = () => {
  loginAdmin(logonForm.value.username).then((res) => {
    options.value = res;
  });
  if (Vue.config.environment) {
    startSign(logonForm.value.username).then((res) => {
      console.log(res, "输入姓名时候，失去焦点");
    });
  }
};

const goback = () => {
  deptId.value = choseList.value.join(",");
  loading.value = true;

  let obj = Object.values(logonForm.value).some(
    (value) => value === null || value === ""
  );

  if (obj) {
    Message.error("请输入账号或密码");
    loading.value = false;
  } else {
    let from = {
      username: logonForm.value.username,
      password: md5(logonForm.value.password),
    };
    localStorage.setItem("deptId", deptId.value);
    login(from).then((res) => {
      if (res.error_code == 400) {
        loading.value = false;
      } else if (res.error_code == 510) {
        Message.error(res.error_description);
        loading.value = false;
      } else {
        localStorage.setItem("token", res.access_token);
        localStorage.setItem("userInfo", JSON.stringify(res));
        loading.value = false;
        router.go(-1);
      }
    })
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.publicBack {
  background-image: url(/src/renderer/assets/loginBack.png);
  background-size: 100% 100%;
  background-attachment: fixed;
}

.showSelet {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 78px;

  >div {
    display: flex;
    flex-flow: column;
  }

  .login_title {
    padding-left: 40px;
    font-family: MicrosoftYaHei;
    line-height: 80px;
    color: #0062ff;
  }

  .login_btn {
    padding: 25px 20px 30px 20px;
    width: 280px;
    border-radius: 3px;
    background: #ffffff;
    align-items: center;
    justify-content: space-between;
  }
}

.login_describe {
  width: 192px;
  height: 33px;
  text-align: center;

  font-family: MicrosoftYaHei, MicrosoftYaHei;
  font-weight: bold;
  font-size: 15px;
  color: #0062ff;
  line-height: 14px;
  font-style: normal;
  border-bottom: 2px solid #0062ff;
}

.login_input {
  margin-top: 38px;
  // background-color: red;
  height: 175px;
  display: flex;
  flex-flow: column;
  justify-content: space-between;
}

.login_btns {
  margin-top: 18px;
  text-align: center;
  width: 214px;
  background: #0062ff;
  border-radius: 15px;

  font-family: MicrosoftYaHei, MicrosoftYaHei;
  font-weight: bold;
  font-size: 12px;
  color: #ffffff;
  font-style: normal;
}

.forty_seven {
  font-size: 47px;
  font-family: YouSheBiaoTiHei;
  letter-spacing: 1px;
}

.thirty_six {
  font-size: 36px;
  letter-spacing: 1px;
}

.twenty {
  font-size: 20px;
}
</style>
